package com.jsbs.iam.auth.controller;

import com.jsbs.iam.auth.constant.Constants;
import com.jsbs.iam.auth.dto.IamAuthDto;
import com.jsbs.iam.auth.service.IamAuthService;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author junwc
 * @className AuthApiController
 * @date 2022/12/27 11:22
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/iam/auth/api")
public class AuthApiController extends BaseController{

    @Autowired
    private IamAuthService iamAuthService;

    /**
     * 功能描述：统一权限校验接口
     * @param request
     * @return
     */
    @RequestMapping("/access/check")
    public Result accessAuthCheck (HttpServletRequest request, @RequestBody IamAuthDto iamAuthDto) {
        try {
            log.info("Route=>AuthApiController method=>accessAuthCheck 统一权限校验入参：{}", Json.toJsonString(iamAuthDto));
            return iamAuthService.accessAuthCheck(iamAuthDto);
        } catch (Exception e) {
            log.error("Route=>AuthApiController method=>accessAuthCheck 统一权限校验异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：统一获取应用系统功能权限列表接口
     * @param request
     * @return
     */
    @RequestMapping("/permisson/list")
    public Result queryForPermissonList (HttpServletRequest request) {
        try {
            // 请求头获取用户编码
            String userCode = request.getHeader(Constants.USER_CODE);
            // 请求头获取归属租户
            String companyCode = request.getHeader(Constants.COMPANY_CODE);
            // 请求头获取应用编码
            String applyCode = request.getHeader(Constants.APPLY_CODE);
            // 用户类型
            String userType = request.getHeader(Constants.USER_TYPE);
            // 默认为普通用户
            if (StringUtils.isEmpty(userType)) {
                userType = Constants.USER_TYPE_NOMAL;
            }
            // 管理员角色编码
            String adminRoleCode = request.getHeader(Constants.ADMIN_ROLE_CODE);
            if (StringUtils.isEmpty(adminRoleCode)) {
                return Result.failWithMsg("管理器角色异常");
            }
            IamAuthDto iamAuthDto = new IamAuthDto();
            iamAuthDto.setUserCode(userCode);
            iamAuthDto.setApplyCode(applyCode);
            iamAuthDto.setCompanyCode(companyCode);
            iamAuthDto.setUserType(userType);
            iamAuthDto.setAdminRoleCode(adminRoleCode);
            log.info("Route=>AuthApiController method=>queryForPermissonList 统一获取应用系统功能权限列表入参：{}", Json.toJsonString(iamAuthDto));
            return iamAuthService.queryPermissonList(iamAuthDto);
        } catch (Exception e) {
            log.error("Route=>AuthApiController method=>queryForPermissonList 统一获取应用系统功能权限列表异常：", e);
            return handleException(e);
        }
    }
}
